package com.ruoyi.web.controller.test;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SellingExpenses;
import com.ruoyi.system.service.ISellingExpensesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 销售费用Controller
 * 
 * @author 王帝
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/system/expenses")
public class SellingExpensesController extends BaseController
{
    @Autowired
    private ISellingExpensesService sellingExpensesService;

    /**
     * 导入数据管理列表
     */
    @Log(title = "数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SellingExpenses> util = new ExcelUtil<SellingExpenses>(SellingExpenses.class);
        List<SellingExpenses> dataManagementList = util.importExcel(file.getInputStream());
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = getUsername();
        String message = sellingExpensesService.importData(dataManagementList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SellingExpenses> util = new ExcelUtil<SellingExpenses>(SellingExpenses.class);
        return util.importTemplateExcel("案卷数据");
    }


    /**
     * 查询销售费用列表
     */
    @PreAuthorize("@ss.hasPermi('system:expenses:list')")
    @GetMapping("/list")
    public TableDataInfo list(SellingExpenses sellingExpenses)
    {
        startPage();
        List<SellingExpenses> list = sellingExpensesService.selectSellingExpensesList(sellingExpenses);
        return getDataTable(list);
    }

    /**
     * 导出销售费用列表
     */
    @PreAuthorize("@ss.hasPermi('system:expenses:export')")
    @Log(title = "销售费用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SellingExpenses sellingExpenses)
    {
        List<SellingExpenses> list = sellingExpensesService.selectSellingExpensesList(sellingExpenses);
        ExcelUtil<SellingExpenses> util = new ExcelUtil<SellingExpenses>(SellingExpenses.class);
        util.exportExcel(response, list, "销售费用数据");
    }

    /**
     * 获取销售费用详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:expenses:query')")
    @GetMapping(value = "/{expId}")
    public AjaxResult getInfo(@PathVariable("expId") Integer expId)
    {
        return AjaxResult.success(sellingExpensesService.selectSellingExpensesByExpId(expId));
    }

    /**
     * 新增销售费用
     */
    @PreAuthorize("@ss.hasPermi('system:expenses:add')")
    @Log(title = "销售费用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SellingExpenses sellingExpenses)
    {
        sellingExpenses.setUserId(getUserId());
        sellingExpenses.setDeptId(getDeptId());
        return toAjax(sellingExpensesService.insertSellingExpenses(sellingExpenses));
    }

    /**
     * 修改销售费用
     */
    @PreAuthorize("@ss.hasPermi('system:expenses:edit')")
    @Log(title = "销售费用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SellingExpenses sellingExpenses)
    {
        return toAjax(sellingExpensesService.updateSellingExpenses(sellingExpenses));
    }

    /**
     * 删除销售费用
     */
    @PreAuthorize("@ss.hasPermi('system:expenses:remove')")
    @Log(title = "销售费用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{expIds}")
    public AjaxResult remove(@PathVariable Integer[] expIds)
    {
        return toAjax(sellingExpensesService.deleteSellingExpensesByExpIds(expIds));
    }
}
